package al.xc.common.util;

import java.util.HashMap;
import java.util.Map;

public class SingleUtils {

    private static SingleUtils instance_ = new SingleUtils();

    private static Map<Class<?>, Object> list_ = new HashMap<>();

    public static  <T> T instance(Class<T> classic) {
        synchronized (list_) {
            T result = (T)list_.get(classic);
            if (null != result) { return result; }

            try {
                result = classic.newInstance();
            } catch (Exception ex) {
                result = null;
            }
            list_.put(classic, result);
            return result;
        }
    }

    public static <T> T instance(T object) {
        synchronized (list_) {
            T result = (T)list_.get(object.getClass());
            if (null != result) { return result; }

            list_.put(object.getClass(), object);
            return object;
        }
    }

    public static <T> T instance(Class<T> classic, T object) {
        synchronized (list_) {
            T result = (T)list_.get(classic);
            if (null != result) { return result; }

            list_.put(classic, object);
            return object;
        }
    }
}
